<template>
    <div class="app-container">
        <!--查询表单-->
        <el-form  :inline="true" class="demo-form-inline">
            <el-form-item label="姓名/手机">
                <el-input v-model="searchObj.keyword" placeholder="姓名/手机"/>
            </el-form-item>

            <el-form-item  label="创建时间">
                <el-date-picker
                v-model="searchObj.createTimeBegin"
                type="datetime"
                placeholder="选择开始时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                default-time="00:00:00"
                />
            </el-form-item>
            至
            <el-form-item>
                <el-date-picker
                v-model="searchObj.createTimeEnd"
                type="datetime"
                placeholder="选择截止时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                default-time="00:00:00"
                />
            </el-form-item>

            <el-button type="primary" icon="el-icon-search" @click="getUserInfoList()">查询</el-button>
            <el-button type="default" @click="resetData()">清空</el-button>
        </el-form>

        <!-- 列表 -->
        <el-table v-loading="listLoading" :data="list" stripe style="width: 100%">
            <el-table-column label="序号" width="70" align="center">
                <template slot-scope="scope">
                    {{ (page - 1) * limit + scope.$index + 1 }}
                </template>
            </el-table-column>

            <el-table-column prop="phone" label="手机号"/>
            <el-table-column prop="nickName" label="昵称"/>
            <el-table-column prop="name" label="姓名"/>
            <el-table-column label="状态" prop="param.statusString"/>
            <el-table-column label="认证状态" prop="param.authStatusString" style="color:orange;"/>
            <el-table-column prop="createTime" label="创建时间"/>

            <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                  <router-link :to="'/user/userInfo/show/'+scope.row.id">
                    <el-button type="primary" size="mini">查看</el-button>
                  </router-link>

                  <el-button v-if="scope.row.status == 1" type="primary" size="mini" @click="lockUser(scope.row.id, 0)">锁定</el-button>
                  <el-button v-if="scope.row.status == 0" type="danger" size="mini" @click="lockUser(scope.row.id, 1)">取消锁定</el-button>
                </template>
            </el-table-column>

        </el-table>

        <!-- 分页 -->
        <el-pagination
          @current-change="getUserInfoList"
          @size-change="changeSize"
          background
          :current-page="page"
          :page-size="limit"
          :page-sizes="[5,10,20,30,40,50,100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          style="padding-top: 27px;">
        </el-pagination>
    </div>
</template>
<script>

import userInfo from '../../../api/userInfo'

export default {
    // 定义数据
    data() {
        return {
            listLoading: true, // 数据是否正在加载
            list: null, // banner 列表
            total: 0, // 数据库中的总记录数
            page: 1, // 默认页码
            limit: 10, // 每页记录数
            searchObj: {}, // 查询表单对象
            authSting: '认证中'
        }
    },
    // 当页面加载时获取数据
    created() {
        this.getUserInfoList()
    },
    methods: {
        // 调用api层获取数据库中的数据
        getUserInfoList(page = 1) {
            // console.log('翻页。。。' + page)
            // 异步获取远程数据（ajax）
            this.page = page
                userInfo.getPageList(this.page, this.limit, this.searchObj).then(
                    response => {
                      this.list = response.data.records
                      this.total = response.data.total
                      // 数据加载并绑定成功
                      this.listLoading = false
                      this.authSting = this.list.param.authStatusString;
                    }
            )
        },

        // 当页码发生改变的时候
        changeSize(size) {
            console.log(size)
            this.limit = size
            this.getUserInfoList(1)
        },

        // 重置查询表单
        resetData() {
            console.log('重置查询表单')
            this.searchObj = {}
            this.getUserInfoList()
        },

        // 更改锁定状态
        lockUser(id, status) {
            this.$confirm('此操作将改变用户状态, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // debugger
                userInfo.lockUser(id, status).then(response => {
                    this.getUserInfoList(this.page)
                    if (response.code) {
                        this.$message({
                            type: 'success',
                            message: '操作成功!'
                        })
                    }
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        }
    }
}
</script>
